grafico = {
		dataSet: [], 
		bindEvents: function(){
			$(".calleSelect").change(function(){
				var id = $(this).val();
				formData = {};
				formData.id = id;
				
				if(id != "-1"){
					request = $.ajax({
			    	    url : "DevolverAltura",
			    	    type: "POST",
			    	    data : formData,
			    	});
					request.done(function (response, textStatus, jqXHR){
						$(".alturaSelect option").remove();
						$("select.alturaSelect").append("<option value=\"-1\">Altura</option>");
						$.each(response.alturas , function(i, v){
							$("select.alturaSelect").append("<option value=\""+v.id+"\">"+v.nro+"</option>");
						});
						$("select.alturaSelect").val(-1);
					});
					$("select").select2({dropdownCssClass: 'dropdown-inverse' });
				}
			});
			
			$(".btnRefresh").click(function(){
				var idCriterio = $("select.criterioSelect").val();
				var idAltura = $("select.alturaSelect").val();
				var cantDias = $(".diasGrafico").val();
				if(idCriterio != "-1" && idAltura != "-1" && cantDias != "" && cantDias > 0){
					var formData = {};
					formData.idCriterio = idCriterio;
					formData.idAltura = idAltura;
					formData.dias = cantDias;
					
					request = $.ajax({
			    	    url : "EvolucionCalleCriterio",
			    	    type: "POST",
			    	    data : formData,
			    	});
					request.done(function (response, textStatus, jqXHR){
						grafico.dataSet = [];
						if(response.puntos && response.puntos.length > 0){
							$("#grafico div.vcenter").hide();
							$.each(response.puntos , function(i, v){
								grafico.dataSet.push({
									date: v.fecha,
									duration: v.valorFuncion,
									lineColor: v.color
								});
							});
							grafico.chart();
						}else{
							grafico.mostrarNotificacion("No existen registros para los campos seleccionados", "top right", "error");
						}
						
					});
			  }else{
				  grafico.mostrarNotificacion("Error: Por favor complete de manera correcta los parametros", "top right", "error");
			  }
			});
		},
		
		chart: function(){ 
			
			return AmCharts.makeChart("grafico", {
			    "type": "serial",
				"theme": "none",
			    "pathToImages": "http://www.amcharts.com/lib/3/images/",
			    "startEffect": "easeOutSine",
			    "dataProvider": grafico.dataSet,
			    "graphs": [{
			        "id": "g1",
			        "bullet": "square",
			        "bulletBorderAlpha": 1,
			        "bulletBorderThickness": 1,
			        "fillAlphas": 0.3,
			        "fillColorsField": "lineColor",
			        "legendValueText": "[[duration]]",
			        "lineColorField": "lineColor",
			        "title": "duration",
			        "valueField": "duration"
			    }],
			     "chartScrollbar": {
						"graph": "g1",
						"scrollbarHeight": 30,
			    },
			    "chartCursor": {
			        "categoryBalloonDateFormat": "YYYY-MM-DD JJ:NN:SS",
			        "cursorAlpha": 0,
			        "zoomable": false
			    },
			    "dataDateFormat": "YYYY-MM-DD JJ:NN:SS",
			    "categoryField": "date",
			    "categoryAxis": {
			        "minPeriod": "mm",
			        "parseDates": true,
			    }
			});
		},
		
		mostrarNotificacion: function(str, position, type){
			$.notify(str, { globalPosition: position,
				  		className: type,
				  		clickToHide: true,
				  		autoHide: true,
				  		autoHideDelay: 4000,
			     	  });
		},
};

$(document).ready(function(){
	$(".reportes").addClass("active");
	grafico.bindEvents();
	$("select").select2({dropdownCssClass: 'dropdown-inverse' });
	
	var total = $("body").height();
	var page_header = $(".page-header").height() + 121;
	var parametros = $(".row.parametros").height();
	
	var graficoHeight = total - page_header - parametros;
	$("#grafico").height(graficoHeight);
	
	$(window).resize(function() {
		  var total = $("body").height();
		  var page_header = $(".page-header").height() + 121;
		  var parametros = $(".row.parametros").height();
		  var graficoHeight = total - page_header - parametros;
		  $( "#grafico" ).height(graficoHeight);
		  if(grafico.dataSet.length > 0){
			  grafico.chart();  
		  }
		  
	});
	$('#btnPopover').click(function(){
		$('.form-control').focus();
		$('.account').popover({
	        title: '<span class="popoverTit">Perfil</span>',
	        content: $('.accountHide').html(),
	        placement: 'bottom',
	        html: true
	    });
	});
});